<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use  App\Models\Phone;
use  App\Models\Weixin;
use  App\Models\PhoneGroup;
use  App\Models\User;

use \GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;


class PhoneController extends Controller
{
    public function list(Request $request)
    {
        $input = Input::only('page', 'limit','message','status','group');
        $validator = Validator::make($input, [        
            'page'      => 'required|string',
            'limit'         => 'required|string',
            'message'     =>  'nullable|string',
            'status'     =>  'nullable|string',
            'group'     =>  'nullable|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $phone = Phone::where('id','>',0);
        $page = $input['page'];
        $limit = $input['limit'];
        if(isset($input['message'])){
            if(strlen($input['message'])>10){
                $phone->where('imei',$input['message']); 
            }else{
                $phone->where('id',$input['message']); 
            }
        }
        if(isset($input['status'])){
            if($input['status'] == 'on'){
                $phone->where('client_id','>',0); 
            }else{
                $phone->where('client_id','=',''); 
            }
        }
        if(isset($input['group'])){
            $phone->where('group_id',$input['group']); 
        }
        $count = $phone->count();
        $data = $phone->select('id','imei','remark','wechat_uid','group_id','user_id','client_id','version','lat','lng','battery')
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->get();
        $temp = [];
        foreach ($data as $key => $value) {
            $temp_data = $value;
            $temp_data['remark'] = $value['remark']??'';
            if($value['wechat_uid']){
                $weixin = Weixin::where('uid',$value['wechat_uid'])->first();
                $temp_data['wechat_name'] = $weixin['nickname'];
                $temp_data['wechat_sex'] = $weixin['sex'];
                $temp_data['wechat_logo'] = $weixin['small_head_url'];
            }else{
                $temp_data['wechat_uid'] = '';
                $temp_data['wechat_name'] = '';
                $temp_data['wechat_sex'] = '';
                $temp_data['wechat_logo'] = '';
            }
            if($value['group_id']){
                $group = PhoneGroup::where('id',$value['group_id'])->first();
                $temp_data['group_name'] = $group['name'];
            }else{
                $temp_data['group_id'] = '';
                $temp_data['group_name'] = '';
            }
            if($value['user_id']){
                $user = User::where('id',$value['user_id'])->first();
                $temp_data['user_name'] = $user['name'];
            }else{
                $temp_data['user_id'] = '';
                $temp_data['user_name'] = '';
            }
            if($value['client_id']){
                $temp_data['status'] = Gateway::isOnline($value['client_id'])?'on':'off'; 
                if($temp_data['status']=='off'){
                    $t = Phone::where('id',$value['id'])->first();
                    $t->client_id = '';
                    $t->save();
                }
            }
            unset($temp_data['client_id']);
            $temp[] = $temp_data;
        }
        $fin['data'] = $temp;
        $fin['count'] = $count;

        return $this->returnOk($fin);
    }

    public function setRemark(Request $request)
    {
        $input = Input::only('id', 'remark');
        $validator = Validator::make($input, [        
            'id'      => 'required|integer',
            'remark'         => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $phone = Phone::where('id',$input['id'])->select('id','remark')->first();
        if($phone){
            $phone->remark = $input['remark']??'';
            $phone->save();
            return $this->returnOk();
        }else{
            return $this->returnError([],409,'手机不存在');
        }
    }

    public function setGroup(Request $request)
    {
        $input = Input::only('id', 'group_id');
        $validator = Validator::make($input, [        
            'id'      => 'required|integer',
            'group_id'         => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $phone = Phone::where('id',$input['id'])->select('id','remark','group_id')->first();
        if($phone){
            $group = PhoneGroup::where('id',$input['group_id'])->first();
            if($group){
                $group->phone_account = $group->phone_account+1;
                $group->save();
            }
            if($phone->group_id == $input['group_id']){
                return $this->returnOk();
            }
            if($phone->group_id){
                $group_before = PhoneGroup::where('id',$phone->group_id)->first();
                $group_before->phone_account = $group_before->phone_account-1;
                $group_before->save();
            }
            $phone->group_id = $input['group_id'];
            $phone->save();
            return $this->returnOk();
        }else{
            return $this->returnError([],409,'手机不存在');
        }
    }

    public function delete()
    {
        $input = Input::only('id');
        $validator = Validator::make($input, [   
            'id'      => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
       
        $where['id'] = $input['id'];
        $exist = Phone::where($where)->first();
        if($exist){
            $exist->delete();
        }else{
            return $this->returnError([],409,'手机不存在');
        }
        return $this->returnOk();
    }

    public function setUser()
    {
        $input = Input::only('id', 'user_id');
        $validator = Validator::make($input, [        
            'id'      => 'required|integer',
            'user_id'         => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $phone = Phone::where('id',$input['id'])->select('id','user_id')->first();
        if($phone){
            $phone->user_id = $input['user_id'];
            $phone->save();
            return $this->returnOk();
        }else{
            return $this->returnError([],409,'手机不存在');
        }
    }
}